﻿using System;
using System.Data;
using System.Drawing.Printing;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Billing.Utilities;

namespace Billing.Windows
{
    /// <summary>
    /// Interaction logic for CakeBalance.xaml
    /// </summary>
    public partial class CakeDelivery : Window
    {
        DataTable dt;
        Color cursorEnterColor = Colors.Violet;
        Color cursorLeaveColor = Color.FromArgb(Convert.ToByte("FF", 16), Convert.ToByte("65", 16), Convert.ToByte("65", 16), Convert.ToByte("65", 16));

        public CakeDelivery()
        {
            try
            {
                InitializeComponent();
                txtBillNo.Focus();

                for (int cnt = 0; cnt > -20; cnt--)
                {
                    dtBookedDt.Items.Add(new ListBoxItem { Tag = DateTime.Now.AddDays(cnt), Content = DateTime.Now.AddDays(cnt).DayOfWeek.ToString() + " " + DateTime.Now.AddDays(cnt).ToString("dd-MMM-yyyy") });
                }
                dtBookedDt.SelectedIndex = 0;
            }
            catch (Exception) { }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
                else if (e.Key == Key.Enter && txtBillNo.Text.Trim() != string.Empty)
                {
                    dt = Database.GetDetailsByCakeBillNo(txtBillNo.Text, DateTime.Parse((dtBookedDt.SelectedItem as ListBoxItem).Tag.ToString()));
                    if (dt != null && dt.Rows != null && dt.Rows.Count > 0)
                    {
                        lblError.Visibility = Visibility.Collapsed;
                        lblBalance.Content = dt.Rows[0][1].ToString();
                    }
                    else
                    {
                        lblError.Visibility = Visibility.Visible;
                        lblError.Content = "Invalid Cake Details";
                        lblBalance.Content = string.Empty;
                    }
                }
                else if (e.Key == Key.F1 && lblBalance.Content.ToString() != string.Empty)
                {
                    if (MessageBox.Show("Do you want to pay the Cake Balance?", "Pay Balance", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.OK) == MessageBoxResult.OK)
                    {
                        if (Database.DeliverCake(dt.Rows[0][0].ToString(), DateTime.Parse((dtBookedDt.SelectedItem as ListBoxItem).Tag.ToString())))
                        {
                            //Print Out & Close
                            CakeDeliveryPrinter printer = new CakeDeliveryPrinter(dt.Rows[0][0].ToString(), txtBillNo.Text, dt.Rows[0][1].ToString());
                            printer.printDocument1.PrintController = new StandardPrintController();
                            printer.printDocument1.Print();
                            this.Close();
                        }
                    }
                }

                //else if (e.Key == Key.Enter && txtCakeBalance.Text.Trim() != string.Empty)
                //{
                //    if (MessageBox.Show("Do you want to create a Cake Balance?", "Cake Balance", MessageBoxButton.OKCancel, MessageBoxImage.Question, MessageBoxResult.OK) == MessageBoxResult.OK)
                //    {
                //        double val = 0d;
                //        if (double.TryParse(txtCakeBalance.Text, out val))
                //        {
                //            if (Database.AddCakeBalance(txtCakeBalance.Text) >= 0)
                //            {
                //                //Print Out
                //                CakeBalancePrinter printer = new CakeBalancePrinter(txtCakeBalance.Text);
                //                printer.printDocument1.PrintController = new StandardPrintController();
                //                printer.printDocument1.Print();
                //            }
                //        }
                //    }
                //    this.Close();
                //}
            }
            catch (Exception) { }
        }

        private void txtBillNo_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Right)
                {
                    dtBookedDt.Focus();
                }
            }
            catch (Exception) { }
        }

        private void dtBookedDt_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Left)
                {
                    txtBillNo.Focus();
                    txtBillNo.SelectAll();
                }
            }
            catch (Exception) { }
        }
    }
}
